package com.swotplus.framework.util;




import com.google.common.collect.Multimap;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;

/**
 *
 */
public class DictionaryUtil {
    private static DictionaryUtil dict = null;

    private DictionaryUtil() {
    }

    public static synchronized DictionaryUtil init() {
        if (dict == null) {
            dict = new DictionaryUtil();
        }
        return dict;
    }

    /**
     * 根据key获取数据字典名称
     * @param dictName 数据字典类型
     * @param dicVal 数据字典key
     * @return
     */
    public static String getDictMemoByVal(String dictName, String dicVal){

        if(!StringUtils.isNotEmpty(dicVal)||DictMap.dictMap==null||DictMap.dictMap.get(dictName)==null){
            return "";
        }
        Collection<String[]> vals=DictMap.dictMap.get(dictName);
        for(String[] strs:vals){
            String dic_value = strs[0];
            String dic_memo = strs[1];
            if(dicVal.equals(dic_value)){
                return dic_memo;
            }
        }
        return "";
    }

    /**
     * 根据dic_type生产select下拉列表
     *
     * @param dic_type
     * @param default_value
     * @return
     */
    public String getOptions(String dic_type, String default_value) {
        if (StringUtils.isBlank(dic_type) || DictMap.dictMap == null || !DictMap.dictMap.containsKey(dic_type)) {
            return "<option></option>";
        }
        Collection<String[]> vals = DictMap.dictMap.get(dic_type);
        StringBuffer sb = new StringBuffer();
        sb.append("<option value=\"").append("\">").append("</option>");
        for (String[] strs : vals) {
            String dic_value = strs[0];
            String dic_memo = strs[1];
            if (!StringUtils.isBlank(default_value) && dic_value.equals(default_value)) {
                sb.append("<option selected value=\"").append(dic_value).append("\">").append(dic_memo).append("</option>");
            } else {
                sb.append("<option value=\"").append(dic_value).append("\">").append(dic_memo).append("</option>");
            }
        }
        return sb.toString();
    }

    /**
     * 根据dic_type生产select下拉列表
     *
     * @param dic_type
     * @param default_value
     * @return
     */
    public String getLjOptions(String dic_type, String default_value) {
        if (StringUtils.isBlank(dic_type) || DictMap.dictMap == null || !DictMap.dictMap.containsKey(dic_type)) {
            return "<option></option>";
        }
        Collection<String[]> vals = DictMap.dictMap.get(dic_type);
        StringBuffer sb = new StringBuffer();
        for (String[] strs : vals) {
            String dic_value = strs[0];
            String dic_memo = strs[1];
            dic_memo = dic_value+" "+strs[1];
            if (!StringUtils.isBlank(default_value) && dic_value.equals(default_value)) {
                sb.append("<option selected value=\"").append(dic_value).append("\">").append(dic_memo).append("</option>");
            } else {
                sb.append("<option value=\"").append(dic_value).append("\">").append(dic_memo).append("</option>");
            }
        }
        return sb.toString();
    }


    public String getLayuiNameRedios(String dic_type, String redioName, String default_value) {
        if (StringUtils.isBlank(dic_type) || DictMap.dictMap == null || !DictMap.dictMap.containsKey(dic_type)) {
            return "";
        }
        Collection<String[]> vals = DictMap.dictMap.get(dic_type);
        StringBuffer sb = new StringBuffer();
        int i=0;
        for (String[] strs : vals) {
            i++;
            String dic_value = strs[0];
            String dic_memo = strs[1];
            if (StringUtils.isNotBlank(default_value) && dic_value.equals(default_value)) {
                sb.append("<input type=\"radio\" name=\""+redioName+"\" value=\""+dic_value+"\" title=\""+dic_memo+"\" checked>");
            } else {
                sb.append("<input type=\"radio\" name=\""+redioName+"\" value=\""+dic_value+"\" title=\""+dic_memo+"\">");
            }
        }
        return sb.toString();
    }

    /**
     * 根据dic_val获取dic_name
     *
     * @param dic_type
     * @param dic_value
     * @return
     */
    public static String getDicName(String dic_type, String dic_value) {
        if (StringUtils.isBlank(dic_type) || DictMap.dictMap == null || !DictMap.dictMap.containsKey(dic_type)) {
            return "";
        }
        Collection<String[]> vals = DictMap.dictMap.get(dic_type);
        for (String[] strs : vals) {
            if (strs[0].equals(dic_value)) {
                return strs[1];
            }
        }
        return "";
    }

    /**
     * 根据dic_name获取dic_value
     *
     * @param dic_type
     * @param dic_name
     * @return
     */
    public static String getDicValue(String dic_type, String dic_name) {
        if (StringUtils.isBlank(dic_type) || DictMap.dictMap == null || !DictMap.dictMap.containsKey(dic_type)) {
            return "";
        }
        Collection<String[]> vals = DictMap.dictMap.get(dic_type);
        for (String[] strs : vals) {
            if (strs[1].equals(dic_name)) {
                return strs[0];
            }
        }
        return "";
    }

    /**
     *
     * @param dic_type  字典dic_name值
     * @param checkBoxName name属性
     * @param checkVals 选中的值 内容  逗号拼接
     * @param isVerify 是否开启校验
     * @return
     */
    public String getLayuiNameCheckBox(String dic_type, String checkBoxName, String checkVals,String isVerify) {
        if (StringUtils.isBlank(dic_type) || DictMap.dictMap == null || !DictMap.dictMap.containsKey(dic_type)) {
            return "";
        }
        Collection<String[]> vals = DictMap.dictMap.get(dic_type);
        StringBuffer sb = new StringBuffer();
        for (String[] strs : vals) {
            String dic_value = strs[0];
            String dic_memo = strs[1];
            boolean flag=false;
            if(StringUtils.isNotBlank(checkVals)){
                String[] checkValAry = checkVals.split(",");
                for(String val:checkValAry){
                    if(val.equals(dic_value)){
                        flag=true;
                        break;
                    }
                }
            }
            if (flag) {
                sb.append("<input type=\"checkbox\" name=\""+checkBoxName+"\" value=\""+dic_value+"\" title=\""+dic_memo+"\" checked ");
            } else {
                sb.append("<input type=\"checkbox\" name=\""+checkBoxName+"\" value=\""+dic_value+"\" title=\""+dic_memo+"\" ");
            }
            if(StringUtils.isNotBlank(isVerify)&&"1".equals(isVerify)){
                sb.append("lay-verify=\""+checkBoxName+"\" >");
            }else{
                sb.append(" >");
            }
        }
        return sb.toString();
    }

    public String getLayuiSwitch( String checkBoxName,String  laytext,String layfilter, String checkVals ) {
            StringBuffer text=new StringBuffer();
            if(StringUtils.isNotBlank(laytext)){
                text.append("lay-text=").append(laytext) ;
            }

            if(StringUtils.isNotBlank(checkVals)){

                if(checkVals.equals("0")){
                    return  " <input type=\"checkbox\" checked=\"true\" id=\""+checkBoxName+"\"  lay-filter=\""+layfilter+"\"  "+text.toString()+" name=\""+checkBoxName+"\" lay-skin=\"switch\">";
                }else{
                    return  " <input type=\"checkbox\" id=\""+checkBoxName+"\"  lay-filter=\""+layfilter+"\"  "+text.toString()+"   name=\""+checkBoxName+"\" lay-skin=\"switch\">";
                }
            }else{
                return  " <input type=\"checkbox\" id=\""+checkBoxName+"\"   lay-filter=\""+layfilter+"\"  "+text.toString()+" name=\""+checkBoxName+"\" lay-skin=\"switch\">";
            }
    }

}
